//
// Created by guo on 24-8-30.
//

#ifndef BBDEMO_PROTOCOL_H
#define BBDEMO_PROTOCOL_H
#include <stdint.h>
#pragma pack(push)
#pragma pack(1)

enum class BBMsgType : uint8_t{
    UNKNOWN,
    SET,
    GET,
    ERASE
};

enum class BBMsgError : uint8_t{
    NO_ERROR,
    NO_VALUE
};

struct BBMsg{
    uint8_t header[2] = {0xEF,0x06};
    uint8_t type{static_cast<uint8_t>(BBMsgType::UNKNOWN)};
    uint8_t error_code{static_cast<uint8_t>(BBMsgError::NO_ERROR)};
    uint8_t  key[128] = {0};
    uint32_t len{0};
    //checksum在最后

    static void gen_raw_data(BBMsgType type, BBMsgError err, const std::string& key, std::string& raw_data,
                             const std::string& value=""){
        raw_data.resize(sizeof(BBMsg)+value.size());
        BBMsg msg;
        msg.type = static_cast<uint8_t>(type);
        msg.error_code = static_cast<uint8_t>(err);
        msg.len = value.size();
        memcpy(msg.key, key.data(), key.size());
        memcpy((char*)raw_data.data(), &msg, sizeof(BBMsg));
        memcpy((char*)raw_data.data()+sizeof(BBMsg), value.data(), value.size());
    }
};
#pragma pack(pop)
#endif //BBDEMO_PROTOCOL_H
